<html>
<head>
	<meta charset='utf-8' />
	<title>辩证助手</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#Content,.Nav_bottom,#Back_Btn{width:98%;margin:0 auto}

			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#Content,.Nav_bottom,#Back_Btn{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>

<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<body style='padding:0;margin:0;background:#eaa'>

	<!-- 1 Content容器 -->
	<div id='Content' style=''>
		<div style='text-align:right;font-size:12px;margin-top:5px;color:blue'>
			<span onclick=_changeBianZheng(2)>征候辩证</span>　
			<span onclick=_changeBianZheng(3)>经方辩证</span>
		</div>		

		<h1 id='contentTitle' style='color:red;text-align:center' onclick="_jumpHidden('../after/login.html',15)">辩证助手</h1>

		<!--症状筛选-->
		<div style='margin-bottom:20px'>
			<button id='main_type2_viewAdd'>筛选症状</button>
			
			<div id='main_type2_addArea' style='display:none;position:fixed;border:0px solid;left:0;top:0;width:100%;height:100%;overflow-y:scroll'>
				<div style='margin:50px auto;border:1px solid;width:80%;background:#ccc;padding:5px'>
				
					<div id='main_type2_viewZhengZhuangList'>
						<h3>分类名</h3>
						<div style='margin-left:20px;word-break:break-all'>
							<label><input type="checkbox" name='list[]' value='aa' />aa</label>
						</div>
					</div>
				
					<div style='text-align:center;margin:30px'>
						<button id='main_type2_AddBtn'>提交</button>
						<button id='main_type2_CancelBtn'>取消</button>
					</div>
				</div>
			</div>
		</div>

		<!--征候列表-->
		<div id='zhengHou_list' style='margin:50px 0'></div>

	</div><!-- 1 Content容器结束 -->


	<!-- 等待提示框 -->
	<div id='Notice'></div>





</body>
</html>




<script>
var zhengHouData; 		//症候数据容器
var searchList = []; 	//筛选症状临时保存容器
var v = 1; 				//是否显示控制台信息

if(!sessionStorage.sel_val){
	sessionStorage.sel_val = 2;
}
switch(sessionStorage.sel_val){
	case 2:case '2':$('#contentTitle').text('征候辩证');break;
	default:$('#contentTitle').text('经方辩证');

}


//更换辩证项目
function _changeBianZheng(ctype){
	if(sessionStorage.sel_val == ctype){
		return;
	}
	sessionStorage.sel_val = ctype;
	location.reload();
}


$(function(){
//非登陆用户初次获取token
	_getToken(function(){
		//初始化
		_zhengHou_init();
		//筛选症状相关
		$("#main_type2_viewAdd").click(function(){
			$(this).hide();
			$('#main_type2_addArea').show();
			_getClasListLocal();
		});



		//筛选症状提交
		$('#main_type2_AddBtn').click(function(){
			var list = [];
		    $.each($('input:checkbox:checked'),function(){
		        list.push($(this).val());
		    });
		    if(list.length < 1){
		    	searchList = [];
		    }else{
		    	searchList = list;
		    }
		    _main_type2_addAreaInit();	//隐藏筛选框



		    _parseData(zhengHouData);//重新加载并筛选征候


		});
		$('#main_type2_CancelBtn').click(function(){
			_main_type2_addAreaInit();
		});
		function _main_type2_addAreaInit(){
			$('#main_type2_viewAdd').show();
			$('#main_type2_addArea').hide();
		}

	})
});



//获取分类症状信息相关
function _getClasListLocal(){
	//C(localStorage.zhengZhuangList);
	if(localStorage.zhengZhuangList && /^{.*}$/.test(localStorage.zhengZhuangList)){
		if(v)C('zhengZhuangList from local');
		var l1 = JSON.parse(localStorage.zhengZhuangList);
		if(l1.expire + 5000 > new Date().getTime() && /^\[.*\]$/.test(l1.data)){
			_parseZhengZhuangList(JSON.parse(l1.data));	//解析症状集合
		}else{
			_getClassListCache();
		}
	}else{
		_getClassListCache();
	}
}
function _getClassListCache(){
	Ajax({},function(resCache){
		if(v)C('zhengZhuangList from cache');
		if(/^\[.*\]$/.test(resCache)){
			var l1 = JSON.parse(resCache);
			_parseZhengZhuangList(l1);	//解析症状集合
			_localData(resCache,1);
		}else{
			if(v)C('zhengZhuangList from after');
			//后台获取症状集合	fun.js
			_getClassList(function(res){
				if(res.data.length > 0){
					var l1 = res.data;
					_parseZhengZhuangList(l1);	//解析症状集合
					_localData(JSON.stringify(l1),1);
				}
			});
		}
	},'zhengZhuangList');
}

//解析症状集合
function _parseZhengZhuangList(l1){
	var tmp_str = '';
	for(var i=0;i<l1.length;i++){
		tmp_str += "<h3>"+l1[i].name+"</h3>";
		tmp_str += "<div style='margin-left:20px;word-break:break-all'>";
		if(l1[i].subList && l1[i].subList.length > 0){
			for(var j=0;j<l1[i].subList.length;j++){
				tmp_str += "<label style='margin-right:15px'><input type='checkbox' value="+l1[i].subList[j].id+" />"+l1[i].subList[j].name+"</label>";
			}
		}
		tmp_str += "</div>";
	}
	$('#main_type2_viewZhengZhuangList').html(tmp_str);
}


//初始化
function _zhengHou_init(){

	var tmp1 = 'localStorage.zhengHouList_' + sessionStorage.sel_val;
	var tmp3 = eval(tmp1);
	//缓存获取征候数据
	//if(localStorage.zhengHouList){
	if(tmp3 && /^{.*}$/.test(tmp3)){
		switch(sessionStorage.sel_val){
			case 3:case '3':
				var zhengHouList = JSON.parse(tmp3);
			break;
			default:
				var zhengHouList = JSON.parse(tmp3);
		}
		

		if(zhengHouList.expire+5000 > new Date().getTime()){
			//缓存有效
			if(v)C('zhengHouList_'+sessionStorage.sel_val+' from local');
			zhengHouData = JSON.parse(zhengHouList.data);
			_parseData(zhengHouData);
		}else{
			//缓存失效
			_afterOrCache();	//缓存或后台获取征候数据
		}
	}else{
		_afterOrCache();	//缓存或后台获取征候数据
	}

}

//缓存或后台获取征候数据
function _afterOrCache(){
	var path = 'zhengHouList_' + sessionStorage.sel_val;
	Ajax({},function(res_cache){
		var p = /^\[.*\]$/;
		if(p.test(res_cache)){
			if(v)C(path + ' from cache');
			zhengHouData = JSON.parse(res_cache);
			if(zhengHouData && zhengHouData.length > 0){
				_localData(res_cache);	//缓存征候数据到local
				_parseData(zhengHouData);
			}
		}else{
			//后台获取征候数据
			if(v)C(path + ' from after');
			_getZhengHouList(function(res){
				zhengHouData = res.data;
				if(res.data && res.data.length > 0){
					_localData(JSON.stringify(res.data));	//缓存征候数据到local
					_parseData(res.data);
				}
			});
		}
	},path);
}

/*
	#缓存征候数据到local
		res_cache   string  json字串
		type
			false	缓存为 症候
			true 	缓存为 症状
*/

function _localData(res_cache,type){
	var tmpData = {
		expire:new Date().getTime(),
		data:res_cache
	}
	if(type){	//缓存症状
		localStorage.zhengZhuangList= JSON.stringify(tmpData);	
	}else{ 		//缓存征候、经方等
		switch(sessionStorage.sel_val){
			case 3:case '3':
				localStorage.zhengHouList_3 = JSON.stringify(tmpData);
			break;
			default:
				localStorage.zhengHouList_2 = JSON.stringify(tmpData);
		}
	}
}


//解析征候数据 
function _parseData(data){
	//无筛选值则按orders排序并去除加工标识
	for(var i=0;i<data.length;i++){
		data[i].impotent = 0;
		if(data[i].subList && data[i].subList.length > 0){
			for(var k=0;k<data[i].subList.length;k++){
				data[i].subList[k].stat = 0;
			}
		}
	}
	var tmp_orders = '';
	for(var i=0;i<data.length-1;i++){
		for(var j=0;j<data.length-1;j++){
			if(parseInt(data[j].orders) > parseInt(data[j+1].orders)){
				tmp_orders = data[j];
				data[j] = data[j+1];
				data[j+1] = tmp_orders;
			}
		}
	}
	
	//有筛选值则加工关键字并按权重值排序
	if(searchList.length > 0){
		for(var i=0;i<data.length;i++){
			for(var j=0;j<searchList.length;j++){
				if(data[i].subList && data[i].subList.length > 0){
					for(var k=0;k<data[i].subList.length;k++){
						if(data[i].subList[k].id == searchList[j]){
							data[i].subList[k].stat = 1;
							data[i].impotent += 1;
						}
					}
				}
			}
		}
		var tmp_impotent = '';
		for(var i=0;i<data.length-1;i++){
			for(var j=0;j<data.length-1;j++){
				if(parseInt(data[j].impotent) < parseInt(data[j+1].impotent)){
					tmp_impotent = data[j];
					data[j] = data[j+1];
					data[j+1] = tmp_impotent;
				}
			}
		}
	}

	var tmp_str = '';
	for(var i=0;i<data.length;i++){
		tmp_str += "<div style='margin:10px 0;font:normal bold 20px/25px normal'>"+data[i].name+"<span style='margin-left:35px;font-size:12px'>序号："+data[i].orders+"</span></div>";
		tmp_str += "<div style='margin-left:30px;word-break:break-all'>";
				if(data[i].subList && data[i].subList.length > 0){
					for(var j=0;j<data[i].subList.length;j++){
					//	C(data[i].subList[j]);
						//有关键字标识的做变色增粗处理
						if(data[i].subList[j].stat){
							tmp_str += "<span style='margin-right:20px;font:italic bold 18px/18px normal;color:blue'>"+data[i].subList[j].name+"，</span>";
						}else{
							tmp_str += "<span style='margin-right:20px'>"+data[i].subList[j].name+"，</span>";
						}
					}
				}
		tmp_str += "</div>";
	}
	$('#zhengHou_list').html(tmp_str);
}



//非登陆用户初次获取token
function _getToken(fun){
	if(!sessionStorage.token1){
		Ajax2({type:'login'},function(res){
			if(res.stat == 'ok'){
				sessionStorage.token1 = res.token1
				if(fun)fun();
			}
		})
	}else{
		if(fun)fun();
	}
}




/*
	### 隐藏式页面跳转
		说明：5秒内连续点击次数达到要求则触发跳转到指定页面
	url string 	要跳转到的页面路径
	hitCount int 触发跳转需要的连续点击次数

	示例：
		<h1 onclick="_jumpHidden('login.html',5)">助手</h1>
*/
function _jumpHidden(url,hitCount){
	if(parseInt(hitCount) < 1){
		hitCount = 0;
	}
	if(!sessionStorage.hitCount){
		sessionStorage.hitCount = 1;
		sessionStorage.hitCountExpire = new Date().getTime();
	}else{
		sessionStorage.hitCount = parseInt(sessionStorage.hitCount) + 1;
	}

	if(parseInt(sessionStorage.hitCount) > hitCount){
		sessionStorage.hitCount = '';
		if(parseFloat(sessionStorage.hitCountExpire) + 5000 > new Date().getTime()){
			location.href=url;
		}
	}
}

</script>












